<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * M_login
 *
 * This is an M_login of a few basic login interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		Phil Sturgeon
 
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Mlogin extends REST_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('mdl_mlogin');
		$this->load->model('mdl_mapps');
	}
/*	
	function login_get()
    {
        
		
		$rows = array();
		$login = array();
		
		
		$page=$this->get('page');
		$limit=$this->get('lim');
		$key=$this->get('key');
		$value=$this->get('val');
		$countitem = 0;
		if (($key!=null) && ($page!=null) ){
			
			$arrwhere=$key." LIKE '%".$value."%'";
			$query = $this->mdl_login->get_item($limit, $page, $arrwhere);
			$countitem = $this->mdl_login->get_countitem($arrwhere);
		}elseif($limit!=null){
		
			$query = $this->mdl_login->get_item($limit, $page);
			$countitem = $this->mdl_login->get_countitem();
		}else{
			$query = $this->mdl_login->get_item();
			$countitem = $this->mdl_login->get_countitem();
		}
		
		if ($query->num_rows() > 0)
		{
			
			$i=0;
			foreach($query->result_array() as $row)
			{
				$rows[$i]['userid'] = $row['userid'];
				$rows[$i]['username'] = $row['username'];
				$rows[$i]['password'] = $row['password'];
				$rows[$i]['level_id'] = $row['level_id'];

				$i++;
			}
			$data = array(
				'countitem' => $countitem,
				'datas' => $rows
				
			);
		}
		
		//print_r($data);
		
        if($data)
        {
            $this->response($data, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Items could not be found'), 404);
        } 
    }
    */
   /* function login_post()
    {
        //$this->some_model->updatelogin( $this->get('id') );
     
		
$data['username']=$this->post('username'); 
$data['password']=$this->post('password'); 
$data['level_id']=$this->post('level_id'); 



		$query = $this->mdl_login->insert($data);
		
	
		if ($query){
			
			$this->response(array('status' => 'true'),200);
		}else{
			$this->response(array('status' => 'false'));
		}

    }
	*/
	
	function check_mlogin_post(){
		       //$this->some_model->updatelogin( $this->get('id') );
        
		//post
		
		$username=$this->post('username'); 
		$password=$this->post('password'); 
		$mappsidonapps=$this->post('mappsidonapps'); 
		$ip=$this->post('ip'); 
		$device=$this->post('device');
		$os=$this->post('os'); 
		$location=$this->post('location'); 
		$deviceid=$this->post('deviceid'); 
		$devicetoken=$this->post('devicetoken'); 
		$mlogincheck = 0;
		// check login, firsttime, verification, pin
		
		$encrypt=md5($password);
		$arrwhere="username = '" . $username . "' and password = '" . $encrypt . "'";
		$query = $this->mdl_mlogin->get_item(0, 0, $arrwhere);
		
		// --- start pengecekan login
		if ($query->num_rows() > 0)
		{
			$i=0;
			foreach($query->result_array() as $row)
			{
				
				$rows[$i]['username'] = $row['username'];
				$rows[$i]['password'] = $row['password'];
				$rows[$i]['channelid'] = $row['channelid'];
				$rows[$i]['verificationstatus'] = $row['verificationstatus'];
			$rows[$i]['priv'] = $row['priv'];
			

				$i++;
			}
		
			$mlogincheck = 1;
			$verificationstatus = $rows[0]['verificationstatus'];
			$channelid = $rows[0]['channelid'];
			$priv = $rows[0]['priv'];
			
			if($verificationstatus==0) {
			$verificationstatus = 'false';
			$verificationnotification = 'Please check your email, to verification process';
			} else {
			$verificationstatus = 'true';
			$verificationnotification = 'false';
			}
		}
		// --- end pengecekan login

			// login status
	
			if ($mlogincheck==1){
			
			
			
			if($mappsidonapps) {
// kalau mappsid ada
// cek ke tmappsid
			$arrwhere="mappsid = '" . $mappsidonapps .  "'";
			$query = $this->mdl_mapps->get_item(0, 0, $arrwhere);

// KALAU ADA UPDATE KE TCHANNEL
			if($query) {
			$query = $this->mdl_mlogin->updatemappsid($username,$mappsidonapps);
			
			$this->load->helper('jwt_helper');
			$token = array();
			$token['username'] = $username;
			$token['password'] = $password;
			$token['mappsidonapps'] = $mappsidonapps;
			$token=JWT::encode($token, 'dompeto');
			$login = array(array(
				'token' => $token ,
				"verificationstatus" => $verificationstatus,
				"verificationnotification" => $verificationnotification,
				'channelid' => $channelid ,
				"mloginstatus" => 'true',
				'mloginnotification' => 'false', 
				'priv' => $priv,
				"username" => $username,
				'mappsidonapps' => $mappsidonapps ,
				'updatemappsidstatus' => 'true', 
				'debug' => 'login true, mappsid ada di tmapps, update mappsid di tchannel'
				));
						
			} else {

// KALAU NGAK ADA CREATE BARU
			$query = $this->mdl_mapps->insert();
			$query = $this->mdl_mapps->get_lastitem(0, 0);
			$i=0;
			foreach($query->result_array() as $row)
			{
			$rows[$i]['mappsid'] = $row['mappsid'];
			$i++;
			}
			$mappsid = $rows[0]['mappsid'];
			$query = $this->mdl_mlogin->updatemappsid($username,$mappsid);
			$mappsidonapps = $mappsid;
			
			$this->load->helper('jwt_helper');
			$token = array();
			$token['username'] = $username;
			$token['password'] = $password;
			$token['mappsidonapps'] = $mappsidonapps;
			$token=JWT::encode($token, 'dompeto');
			$login = array(array(
				'token' => $token ,
				"verificationstatus" => $verificationstatus,
				"verificationnotification" => $verificationnotification,
				'channelid' => $channelid ,
				"mloginstatus" => 'true',
				'mloginnotification' => 'false', 
				'priv' => $priv, 
				"username" => $username,
				'mappsidonapps' => $mappsidonapps ,
				'updatemappsidstatus' => 'true', 
				'debug' => 'login true, mappsid ngak ada di tmapps, create baru, update mappsid di tchannel'
				));		
				
				$data = array(
				'ip' => $ip,
				'device' => $device,
				'os' => $os,
				'deviceid' => $deviceid,
				'location' => $location,
				'devicetoken'=>$devicetoken,
				'priv'=>$priv
			);

			
			$query = $this->mdl_mlogin->updateipoperator($username,$data);
			}
			} else {
// kalau mappsid null
// create mappsid baru
// update ke tchannel
			$query = $this->mdl_mapps->insert();
			$query = $this->mdl_mapps->get_lastitem(0, 0);
			$i=0;
			foreach($query->result_array() as $row)
			{
			$rows[$i]['mappsid'] = $row['mappsid'];
			$i++;
			}
			$mappsid = $rows[0]['mappsid'];
			$query = $this->mdl_mlogin->updatemappsid($username,$mappsid);
			$mappsidonapps = $mappsid;

			
			$this->load->helper('jwt_helper');
			$token = array();
			$token['username'] = $username;
			$token['password'] = $password;
			$token['mappsidonapps'] = $mappsidonapps;
			$token=JWT::encode($token, 'dompeto');
			$login = array(array(
				'token' => $token ,
				"verificationstatus" => $verificationstatus,
				"verificationnotification" => $verificationnotification,
				'channelid' => $channelid ,
				"mloginstatus" => 'true',
				'mloginnotification' => 'false', 
				"username" => $username,
				'priv' => $priv,
				'mappsidonapps' => $mappsidonapps ,
				'updatemappsidstatus' => 'true', 
				'debug' => 'login true, mappsid null, update mappsid di tchannel'
				));
			}		
			

			
			$data = array(
				'ip' => $ip,
				'device' => $device,
				'os' => $os,
				'deviceid' => $deviceid,
				'location' => $location,
				'devicetoken'=>$devicetoken,
				'priv'=>$priv
			);

			$mappsidstatus = 'true';
			$updatemappsidstatus ='false';

			$query = $this->mdl_mlogin->updateipoperator($username,$data);
		
		// kalau yg login main person
			}else{
			// kalau yg login main peronoperator
			$encrypt=md5($password);
		$arrwhere="username = '" . $username . "' and password = '" . $encrypt . "'";
		$query = $this->mdl_mlogin->get_item_operator(0, 0, $arrwhere);

		if($query->num_rows() > 0) {
		
		$i=0;
			foreach($query->result_array() as $row)
			{
				
				$rows[$i]['username'] = $row['username'];
				$rows[$i]['password'] = $row['password'];
				$rows[$i]['channelid'] = $row['channelid'];
				$rows[$i]['verificationstatus'] = $row['verificationstatus'];
			

				$i++;
			}
		
		$login = array(array(
				'channelid' => $rows[0]['channelid'],
				"mloginstatus" => "true",
				'priv'=>$priv,
				"username" => $rows[0]['username'],
				"mappsidonapps" => $mappsidonapps,
				"mloginnotification" => "success operator",
				'debug'=> '   mlogincheck  operator sukses'
				));
				
				
				$data = array(
				'ip' => $ip,
				'device' => $device,
				'os' => $os,
				'deviceid' => $deviceid,
				'location' => $location,
				'devicetoken'=>$devicetoken,
				'priv'=>$priv
			);

			$mappsidstatus = 'true';
			$updatemappsidstatus ='false';

			$query = $this->mdl_mlogin->updateipoperator($username,$data);
			// kalau yg login operator
				
		} else {
		
		// error
		
			$login = array(array(
				"mloginstatus" => "false",
				"mappsidonapps" => "false",
				"mloginnotification" => "Your username or password wrong",
				'debug'=> '   mlogincheck  false'
				));
			}
			}
		
			
				 $this->response(($login), 200);	
	}
	

    /*
    function login_delete()
    {
		
    	//$this->some_model->deletesomething( $this->get('id') );$this->_get('id')
        $message = array('message' => 'DELETED!', 'id'=>$this->delete('id'));
		
		$query = $this->mdl_login->delete($this->delete('id'));
		//$this->response($message,200);
		//$query=1;
		if ($query){
			$this->response(array('status' => 1),200);
		
		}else{
			$this->response(array('status' => 0));
		}
      
    } */
	/*
	 function login_put()
    {
		
    	//$this->some_model->deletesomething( $this->get('id') );$this->_get('id')
        //$message = array('message' => 'UPDATE!', 'userid'=>$this->delete('userid'), 'name'=>$this->delete('name'));
		$data['userid']=$this->put('userid'); 
$data['username']=$this->put('username'); 
$data['password']=$this->put('password'); 
$data['level_id']=$this->put('level_id'); 

		$query = $this->mdl_login->update($data['userid'],$data);
		//$this->response($message,200);
		
		if ($query){
			$this->response(array('status' => 1),200);
		
		}else{
			$this->response(array('status' => 0));
		}
      
    } */
    /*
    function logins_get()
    {
        //$logins = $this->some_model->getSomething( $this->get('limit') );
        $logins = array(
			array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com'),
			array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com'),
			3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => array('hobbies' => array('fartings', 'bikes'))),
		);
        
        if($logins)
        {
            $this->response($logins, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Couldn\'t find any items!'), 404);
        }
    } */
/*

	public function send_post()
	{
		var_dump($this->request->body);
	}
*/
/*
	public function send_put()
	{
		var_dump($this->put('foo'));
	}
	*/
}